import cTitle from "components/title";

const documentElement = document.documentElement;
const body = document.body;
export default {
  data() {
    return {
      infoData: {},
      continue_days: "", //连续签到天数
      sign_total: "", //累计签到天数
      cumulative: "", //累计奖励
      sign_log: [], //签到记录
      sign_name: "", //签到名字
      integral: "",
      sign_model:"",
      love_name:"",
      love_return: "",//累计爱心返回值
      is_love:false,//是否安装了爱心值插件
      cumulative_point:'',//累计探币
      cumulative_coupon:'',//累计优惠劵
      cumulative_love:'',//累计爱心值
      point:'',
      page: 1,
      total_page: 0,
      isLoadMore: true,
    };
  },
  methods: {
    getData() {
      if(this.page == 1){
        this.sign_log = [];
      }
      $http
        .get(
          "plugin.sign.Frontend.Modules.Sign.Controllers.sign-log.log",
          {
            page:this.page
          },
          "loading"
        )
        .then(res => {
          this.infoData = res.data;
          this.continue_days = res.data.continue_days;
          this.sign_total = res.data.sign_total;
          this.cumulative =
            window.localStorage.integral + res.data.cumulative.substr(2);
          this.integral = window.localStorage.integral;
          this.sign_log = this.sign_log.concat(res.data.sign_log.data);
          // this.sign_log =  [
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          //   {
          //     status: 1,
          //     created_at: "2022-12-09 09:12:28",
          //     award_point: "10.00",
          //     award_coupon: 1,
          //     award_love: 12,
          //     total_love_return: "0.00",
          //     award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
          //   },
          // ],
          this.total_page = res.data.sign_log.total;
          this.sign_name = res.data.sign_name;
          this.love_name = res.data.love_name||'爱心值';
          this.is_love = res.data.is_love;
          this.love_return = res.data.love_return;
          this.sign_model = res.data.sign_model;
          this.fun.setWXTitle(this.sign_name + "记录");
          this.cumulative_love = res.data.sign_model.cumulative_love;
          this.cumulative_coupon = res.data.sign_model.cumulative_coupon;
          this.cumulative_point = res.data.sign_model.cumulative_point;
          this.point = this.fun.getCustomTextLang('member_center.credit1','探币');
          let root = document.querySelector(":root");
          root.style.setProperty("--themeBaseColor", localStorage.getItem('basic_color'));
          if(this.page > res.data.sign_log.last_page){
            this.page  = res.data.sign_log.last_page;
            this.$notify({
              background: "#fef0f0",
              message: `没有更多数据了`,
              color: "#f56c6c"
            });
          }
          this.isLoadMore = true;
        });
    },
    handleScroll(){
      if (
        this.getScrollTop() + this.getClientHeight()  +5 >
        this.getScrollHeight()
      ) {
        // console.log('下拉刷新了')
        //此处发起请求
        if (this.isLoadMore) {
          this.getMoreData();
          console.log('加载更多');
        } else {
          console.log("没有更多数据");
        }
      }
    },
    getMoreData(){
      this.isLoadMore =false;
      this.page ++;
      // this.sign_log = this.sign_log.concat([{
      //   status: 1,
      //   created_at: "2022-12-09 09:12:28",
      //   award_point: "10.00",
      //   award_coupon: 1,
      //   award_love: 12,
      //   total_love_return: "0.00",
      //   award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
      // },            {
      //   status: 1,
      //   created_at: "2022-12-09 09:12:28",
      //   award_point: "10.00",
      //   award_coupon: 1,
      //   award_love: 12,
      //   total_love_return: "0.00",
      //   award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
      // },            {
      //   status: 1,
      //   created_at: "2022-12-09 09:12:28",
      //   award_point: "10.00",
      //   award_coupon: 1,
      //   award_love: 12,
      //   total_love_return: "0.00",
      //   award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
      // },            {
      //   status: 1,
      //   created_at: "2022-12-09 09:12:28",
      //   award_point: "10.00",
      //   award_coupon: 1,
      //   award_love: 12,
      //   total_love_return: "0.00",
      //   award_content: "探币：+10.00；优惠券：（1）张；爱心值a：(12)"
      // },]);
      // this.isLoadMore = true;
      this.getData();
    },
    currentChange(){},
    continueDays(){
      if(this.continue_days == '暂无') {
        return  0;
      }  else { return this.continue_days.substring(0,this.continue_days.length-1);}
    },
    //获取滚动条当前的位置
    getScrollTop() {
      var scrollTop = 0;
      if (documentElement && documentElement.scrollTop) {
        scrollTop = documentElement.scrollTop;
      } else if (body) {
        scrollTop = body.scrollTop;
      }
      return scrollTop;
    },
    //获取当前可视范围的高度
    getClientHeight() {
      var clientHeight = 0;
      if (body.clientHeight && documentElement.clientHeight) {
        clientHeight = Math.min(
          body.clientHeight,
          documentElement.clientHeight
        );
      } else {
        clientHeight = Math.max(
          body.clientHeight,
          documentElement.clientHeight
        );
      }
      return clientHeight;
    },
    getScrollHeight() {
      return Math.max(body.scrollHeight, documentElement.scrollHeight);
    },
    initData(){
      this.page = 1;
      this.total_page = 0;
      this.isLoadMore = true;
    }
  },
  activated() {
    this.initData();
    window.addEventListener("scroll", this.handleScroll);
  },
  deactivated() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  mounted(){
  },
  created() {
    this.getData();
  },
  watch: {
    $route(to) {
      if (to.name == "signrecord") {
        this.getData();
      }
    }
  },
  computed:{
    signTotal(){
      return this.sign_total.substring(0,this.sign_total.length-1);
    },
  },
  components: { cTitle }
};
